<?php
/*
Plugin Name: Document Centre
Version: 0.1.0
Description: Allows authorised members (by default this would include authors) to upload documents to a common document pool which can be accessed by authenticated users. This can all be handled through the front-end of the website for an easier and more intuitive experience.
Author: Barry Hughes
Author URI: http://freshlybakedwebsites.net
*/


/**
 * Primary Document Centre class.
 */
class DocCentre
{
	/**
	 * Plugin location variables.
	 */
	public $assets_url;
	public $includes_dir;
	public $plugin_dir;
	public $plugin_file;
	public $plugin_url;
	public $templates_dir;
	
	/**
	 * Document library and management object.
	 * 
	 * @var DocCentre_Library
	 */
	public $library;
	
	/**
	 * Document server object.
	 * 
	 * @var DocCentre_Server
	 */
	public $server;
	
	
	
	/**
	 * Initializes Document Centre plugin for WordPress.
	 * 
	 * @return DocCentre
	 */
	public function __construct()
	{
		// Basic setup
		$this->locate();
		$this->includes();
		
		// Principle classes
		$this->library = new DocCentre_Library($this);
		$this->server = new DocCentre_Server($this);
		
		// Shortcodes
		new DocCentre_Directory_Shortcode($this);
		new DocCentre_Upload_Shortcode($this);
	}
	
	
	/**
	 * Determines the location of the plugin as a URL and in relation
	 * to the filesystem; also defines a number of convenience variables
	 * containing other locations.
	 */
	protected function locate()
	{
		// Primary locations
		$this->plugin_dir = dirname(__FILE__);
		$this->plugin_file = basename(__FILE__);
		$this->plugin_url = trailingslashit(WP_PLUGIN_URL).basename($this->plugin_dir);
		
		// Other notable locations
		$this->assets_url = trailingslashit($this->plugin_url).'assets';
		$this->includes_dir = trailingslashit($this->plugin_dir).'includes';
		$this->templates_dir = trailingslashit($this->plugin_dir).'templates';
	}
	
	
	/**
	 * Loads the rest of the plugin code.
	 */
	protected function includes()
	{
		// Base path
		$includes = trailingslashit($this->includes_dir);
		
		// Components
		require $includes.'library.php';
		require $includes.'server.php';
		require $includes.'shortcodes/directory.php';
		require $includes.'shortcodes/upload.php';
	}
	
	
	/**
	 * Determines and returns the current request URL string
	 * complete with scheme.
	 */
	public function request_url()
	{		
		// Form the basic URL
		$url = get_home_url();
		$url = trailingslashit($url)
		     . $GLOBALS['wp']->request;
		     
		// Secure scheme?
		if (is_ssl()) str_replace('http://', 'https://', $url);
		
		// Allow plugins to modify returned URL (useful if the
		// URL query needs to remain in special cases, etc)
		return apply_filters('doccentre_request_url', $url);
	}
	
	
	/**
	 * Loads the desired template file and extracts the optional array
	 * of template variables, bringing them into scope. The template is
	 * rendered and returned, it is not outputted directly.
	 * 
	 * @param string $template
	 * @param array $vars = null
	 * @return string
	 */
	public function load_template($template, array $vars = null)
	{
		// Form the path
		$path = trailingslashit($this->templates_dir).$template.'.php';
		
		// If it doesn't exist, throw an exception
		if (!file_exists($path))
			throw new Exception("Cannot load nonexistent template file: $path.");
			
		// If set, bring the $vars array into scope and extract
		if ($vars !== null)
			extract($vars);
			
		// Include the template within output buffer
		ob_start();
		include $path;
		
		// Get contents and clean buffer - then return the contents
		return ob_get_clean();
	}
}


// Launch
new DocCentre;